import { defineStore } from "pinia";
import { RouteRecordNormalized, RouteLocationMatched } from "vue-router";
export const useGlobSet = defineStore({
  id: "globSet",
  state: () => {
    return {
      /**
       * 是否折叠侧边菜单
       * true为收起菜单
       * false为展开菜单
       */
      isCollapse: false,
      /**
       * 判断是否是移动端
       * true为移动端
       * false为PC端
       */
      isMobile: false,
      breadcrumb: [] as RouteRecordNormalized[], //面包屑
    };
  },
  getters: {
    /**
     * 获取面包屑
     * @param state
     * @returns
     */
    getBreadcrumb(state) {
      return state.breadcrumb;
    },
  },
  actions: {
    // TODO 有待改进，如果当前的路由子元素不止一个且alwaysShow为true时，目前没有解决该问题
    setBreadcrumb(matched: RouteLocationMatched[], alwaysShow: boolean) {
      if (alwaysShow && matched.length >= 2) {
        this.breadcrumb = [JSON.parse(JSON.stringify(matched[1]))];
      } else if (matched.length >= 2) {
        this.breadcrumb = JSON.parse(JSON.stringify(matched));
      }
    },
  },
  persist: {
    enabled: true,
  },
});
